package com.harmony.pilot.completion.controller;

import com.harmony.pilot.common.dto.Diagnostic;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.completion.service.DiagnosticService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 诊断控制器
 */
@Slf4j
@RestController
@RequestMapping("/completion/diagnostic")
@RequiredArgsConstructor
public class DiagnosticController {
    
    private final DiagnosticService diagnosticService;
    
    /**
     * 检查文件错误
     */
    @PostMapping("/check")
    public RestResult<List<Diagnostic>> checkFile(@RequestBody DiagnosticRequest request) {
        log.debug("Checking file: {}", request.getFilePath());
        
        try {
            List<Diagnostic> diagnostics = diagnosticService.checkFile(
                request.getFilePath(),
                request.getCode(),
                request.getProjectRoot()
            );
            
            return RestResult.success(diagnostics);
        } catch (Exception e) {
            log.error("Failed to check file", e);
            return RestResult.error("Failed to check file: " + e.getMessage());
        }
    }
    
    /**
     * 检查语法错误
     */
    @PostMapping("/syntax")
    public RestResult<List<Diagnostic>> checkSyntax(@RequestBody DiagnosticRequest request) {
        log.debug("Checking syntax: {}", request.getFilePath());
        
        try {
            List<Diagnostic> diagnostics = diagnosticService.checkSyntax(
                request.getFilePath(),
                request.getCode(),
                request.getProjectRoot()
            );
            
            return RestResult.success(diagnostics);
        } catch (Exception e) {
            log.error("Failed to check syntax", e);
            return RestResult.error("Failed to check syntax: " + e.getMessage());
        }
    }
    
    /**
     * 检查类型错误
     */
    @PostMapping("/types")
    public RestResult<List<Diagnostic>> checkTypes(@RequestBody DiagnosticRequest request) {
        log.debug("Checking types: {}", request.getFilePath());
        
        try {
            List<Diagnostic> diagnostics = diagnosticService.checkTypes(
                request.getFilePath(),
                request.getCode(),
                request.getProjectRoot()
            );
            
            return RestResult.success(diagnostics);
        } catch (Exception e) {
            log.error("Failed to check types", e);
            return RestResult.error("Failed to check types: " + e.getMessage());
        }
    }
    
    /**
     * 检查编译错误
     */
    @PostMapping("/compilation")
    public RestResult<List<Diagnostic>> checkCompilation(@RequestBody DiagnosticRequest request) {
        log.debug("Checking compilation: {}", request.getFilePath());
        
        try {
            List<Diagnostic> diagnostics = diagnosticService.checkCompilation(
                request.getFilePath(),
                request.getCode(),
                request.getProjectRoot()
            );
            
            return RestResult.success(diagnostics);
        } catch (Exception e) {
            log.error("Failed to check compilation", e);
            return RestResult.error("Failed to check compilation: " + e.getMessage());
        }
    }
    
    @Data
    public static class DiagnosticRequest {
        private String filePath;
        private String code;
        private String projectRoot;
    }
}

